/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * author Svetlana Shikhutskaya
 */
var roles="";
var userExists;
var login;
var fname;
var lname;
var address;
var password;
var confirm;

function addUser(page){    
    //        alert("start addUser");
    if(page=="admin"){
        roles=fillRoles(roles);
    }
    else{
        if(page=="registration")
            roles="";
    }
    //    alert("start post");
    $.post("AddUser",{
        Login:document.getElementById("Login").value,
        First_Name:document.getElementById("First_Name").value,
        Last_Name:document.getElementById("Last_Name").value,
        E_mail:document.getElementById("E_mail").value,
        Address:document.getElementById("Address").value,
        Password:document.getElementById("Password").value,
        Roles:roles
    },
    function(result){
        if(result=='true'){
            roles="";
            if(page=="admin"){
                window.location.href="userview";
            }
            if(page=="registration"){
                window.location.href="main.jsp";
            }
        }
        else{
            document.getElementById('warningConfirm').innerHTML = "We couldn't create such user";
        }
    })
}

function fillRoles(line){
    //    alert("fillRoles admin.js");
    if (document.getElementById("Admin").checked){
        line=addComma(line);
        line+="Administrator";
    }
    if (document.getElementById("Warehouse_Employee").checked) {
        line=addComma(line);
        line+="Warehouse Employee";
    }
    if (document.getElementById("Courier").checked){
        line=addComma(line);
        line+="Courier";
    }
    if (document.getElementById("Customer").checked){
        line=addComma(line);
        line+="Customer";
    }
//    console.log(line);
    //    alert("finish fillRoles");
    return line;
}

function addComma(line){
    if(line!="")
        line+=",";
    return line;
}

function checkFName(){
   //   alert('checkFName');
    var regex=new RegExp("^[a-zA-Z]+$");
    var str=document.getElementById("First_Name").value;
    if(str.length>0){
        if(regex.test(str)){
            document.getElementById("warningFName").innerHTML='';
            return true;
        } else 
            document.getElementById("warningFName").innerHTML='You can use only letters (a-z)';
        return false;
    }
    else{
        document.getElementById("warningFName").innerHTML='Enter your first name';
        return false;
    }
}

function checkLName(){
    //    alert('checkLName');
    var regex=new RegExp("^[a-zA-Z]+$");
    var str=document.getElementById("Last_Name").value;
    if(str.length>0){
        if(regex.test(str)){
            document.getElementById("warningLName").innerHTML='';
            return true;
        } else 
            document.getElementById("warningLName").innerHTML='You can use only letters (a-z)';
        return false;
    }
    else{
        document.getElementById("warningLName").innerHTML='Enter your last name';
        return false;
    }
}

function checkLogin(){
    //    alert('checkLogin');
    var str=document.getElementById("Login").value;
    var regex=new RegExp("^[0-9a-zA-Z_,.\\s]+$");
    if(str.length>0){
        if(regex.test(str)){
            if(str.length>=5){
                document.getElementById("warningLogin").innerHTML='';
                $.post("CheckLogin", {
                    Login:document.getElementById("Login").value
                }, function(result){
//                    console.log(result);
                    if (result=="false"){
//                        alert('fail');
                        document.getElementById('warningLogin').innerHTML = 'User with such login already exists';
                        login=false;
                        letAdd();
                        return false;
                    }
                    else{
//                        alert('ok');
                        document.getElementById('warningLogin').innerHTML = '';
                        login=true;
                        letAdd();
                        return true;
                    }
                })
            } else{
                document.getElementById("warningLogin").innerHTML='Too short login';
                return false;
            }
        } else{
            document.getElementById("warningLogin").innerHTML='You can use only letters (a-z), numbers, dots, komas and underscore characters';
            return false;
        }
    } else{
        document.getElementById("warningLogin").innerHTML='Enter your login';
        return false;
    }
}

function checkAddress(){
    //    alert('checkAddress');
    var str=document.getElementById("Address").value;
    var regex=new RegExp("^[0-9a-zA-Z,./\\s]+$");
    if(str.length>0){
        if(regex.test(str)){
            document.getElementById("warningAddress").innerHTML='';
            return true;
        } else{
            document.getElementById("warningAddress").innerHTML='You can use only letters (a-z), numbers, dots, komas and slashes';
            return false;
        }
    } else{
        document.getElementById("warningAddress").innerHTML='Enter your address';
        return false;
    }
}

function validate(){
    fname=checkFName();
    lname=checkLName();
    address=checkAddress();
    password=checkPassword();
    confirm=checkConfirmation();
    login=checkLogin();
}

function letAdd(){
//    console.log('login:'+login,'fname:'+fname,'lname:'+lname,'address:'+address,'password:'+password,'confirm:'+confirm);
    if(login && fname && lname && address && password && confirm){
        document.getElementById("ok").disabled=false;
    }
    else{
        document.getElementById("ok").disabled=true;
    }
}

function checkPassword(){
var password=document.getElementById("Password").value;
if(password.length>0){
    if(password.length>=5){
        document.getElementById('warningPassword').innerHTML = '';
        return true;
    }
    else{
        document.getElementById('warningPassword').innerHTML = 'Too short password';
        return false;
    }
} else{
    document.getElementById('warningPassword').innerHTML = 'Enter your password';
    return false;
}
}

function checkConfirmation(){
var password=document.getElementById("Password").value;
var confirmation =document.getElementById("Confirmation").value;
if(confirmation.length>0){
    if(password==confirmation){
        document.getElementById('warningConfirm').innerHTML = '';
        return true;
    }
    else{
        document.getElementById('warningConfirm').innerHTML = 'Wrong confirmation';
        return false;
    }
} else{
    document.getElementById('warningConfirm').innerHTML = 'Confirm your password';
    return false;
}
}


$(function() {
$(document).on('submit', '.js-adduser-form', function(e) {
    //        alert("press OK add");
    page="admin";
    e.preventDefault();
    addUser(page);      
})
})
$(function(){
$(document).on('submit','.js-registration-form',function(e){
    page="registration";
    //        alert("press OK reg");
    e.preventDefault();
    addUser(page);
})
})
